package com.work.formaldehyde.activity;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.umeng.analytics.MobclickAgent;
import com.wang.avi.AVLoadingIndicatorView;
import com.work.formaldehyde.R;
import com.work.formaldehyde.activityadapter.CityManageAdapter;
import com.work.formaldehyde.activityadapter.JianCeZuoAdapter;
import com.work.formaldehyde.activityadapter.WuLiuAdapter;
import com.work.formaldehyde.model.CityManageModel;
import com.work.formaldehyde.model.WuLiuModel;
import com.work.formaldehyde.util.ApiUtils;
import com.work.formaldehyde.util.Url;

import org.json.JSONObject;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class CheckWuLiuActivity extends Activity implements View.OnClickListener {
    private ImageView comeback, wuliu_img;
    private TextView wuiliushangping, wuliudingdan, wuliuzhuangtai;
    private ListView wuliu_list;
    private AVLoadingIndicatorView loding_avi;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ApiUtils.ActivityIsLogin(this, 0);
        setContentView(R.layout.activity_checkwuliu);
        comeback = findViewById(R.id.jiance_comeback);
        comeback.setOnClickListener(this);
        if (ApiUtils.isNetworkConnected(CheckWuLiuActivity.this))
            init();
    }

    String ordenum = "";

    /**
     * 初始加载
     */
    public void init() {
        Intent getgoods = getIntent();
        ordenum = getgoods.getStringExtra("dingdanhao");
        loding_avi = findViewById(R.id.loding_avi);
        wuiliushangping = findViewById(R.id.wuliushangping);
        wuiliushangping.setText(getgoods.getStringExtra("shangpingname"));
        wuliudingdan = findViewById(R.id.wuliudanhao);
        wuliuzhuangtai = findViewById(R.id.wuliuzhuangtai);
        wuliu_img = findViewById(R.id.wuliu_img);
        ApiUtils.GET_IMAGE(CheckWuLiuActivity.this, getgoods.getStringExtra("imgurl"), wuliu_img);
        wuliu_list = findViewById(R.id.wuliu_list);
        myAdapter = new WuLiuAdapter(new_list_views,
                CheckWuLiuActivity.this);
        wuliu_list.setAdapter(myAdapter);
        okHttp_postFromParameters();
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.jiance_comeback:
                onBackPressed();
                break;
        }
    }

    private void okHttp_postFromParameters() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient okHttpClient = new OkHttpClient();
//                    RequestBody formBody = new FormBody.Builder().add("order_number", ordenum)
//                            .build();
//                    Request request = new Request.Builder().url(Url.GETWULIULIST).post(formBody).build();
//                    okhttp3.Response response = okHttpClient.newCall(request).execute();
//                    String msg = response.body().string();
//                    Message message = new Message();
//                    message.obj = msg;
//                    gethot1.sendMessage(message);
                    Request request = new Request.Builder().get().url(Url.GETWULIU +"?uid="+ Url.USER_ID + "&logintoken=" + Url.LOGIN_TOKEN + "&order_number=" + ordenum).build();
                    Call call = okHttpClient.newCall(request);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                        }

                        @Override
                        public void onResponse(Call call, final Response response) {
                            try {
                                final String responseStr;
                                responseStr = response.body().string();
                                Message message = new Message();
                                message.obj = responseStr;
                                gethot1.sendMessage(message);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } catch (
                        Exception e) {
                    e.printStackTrace();
                }
            }
        }).
                start();
    }

    private ArrayList<WuLiuModel.list> new_list_views = new ArrayList<>();
    private WuLiuModel cityManageModel;
    private WuLiuAdapter myAdapter;
    public Handler gethot1 = new Handler() {
        public void handleMessage(android.os.Message msg) {
            JSONObject success_Object;
            try {
                if (msg.obj != null) {
                    loding_avi.hide();
                    success_Object = new JSONObject(msg.obj.toString());
                    String code = success_Object.getString("code");
                    if (code.equals("1")) {
                        Gson gson = new Gson();
                        cityManageModel = gson.fromJson(msg.obj.toString(),
                                WuLiuModel.class);
                        new_list_views.addAll(cityManageModel.getData().getList());
                        Collections.reverse(new_list_views);
                        myAdapter.notifyDataSetChanged();
                        if (cityManageModel.getData().getStatus().equals("0"))
                            wuliuzhuangtai.setText("运输中");
                        else
                            wuliuzhuangtai.setText("已签收");
                        wuliudingdan.setText(cityManageModel.getData().getCompany() + ":" + cityManageModel.getData().getNo());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
}
